package com.regan.erp.service;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Prop;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.regan.erp.exception.MyBaselogicException;
import com.regan.erp.model.ResultPoJo;
import com.regan.erp.model.User;
import com.regan.erp.util.*;

import java.util.List;
import java.util.Random;

/**
 * 用户Service
 *
 * @author zhangby
 * @date 2018/9/16 上午10:47
 */
public class UserService {

    /**
     * 用户列表查询
     *
     * @return
     * @author zhangby
     * @date 2018/9/16 上午10:50
     */
    public Page<User> getUserListData(Dict paramDict) {
        SqlPara sqlPara = Db.getSqlPara("user.getUserListData", paramDict);
        Page<User> paginate = User.dao.paginate(paramDict.getInt("pageNum"), paramDict.getInt("pageSize"), sqlPara);
        //用户信息预处理
        FunctionUtils.execute(paginate.getList(), User.preConsumer);
        return paginate;
    }

    /**
     * 发送手机验证码
     *
     * @param phone
     * @return
     * @author zhangby
     * @date 2018/9/24 下午4:16
     */
    public ResultPoJo sendPhoneAuthCode(String phone) {
        //随机生成4位数字验证码
        String autoCode = CommonUtil.createAuthCode4(4, true);
        //存入redis
        RedisUtil.set(
                StrUtil.format(ConfigUtil.redisKey().get("authCode_phone"), phone),
                Integer.parseInt(autoCode),
                ConfigUtil.baseConfig().getInt("authcode_phone_timeout"));

        //此处调用短信接口平台，省略。。
        //TODO 调用发送短信接口

        return ResultPoJo.create();
    }

    /**
     * 验证手机短信验证码
     *
     * @param paramDict <br>
     *                  phone 手机号 <br>
     *                  authCode 验证码 <br>
     * @return
     */
    public ResultPoJo verifyPhoneAuthCode(Dict paramDict) {
        //判断是否开启万能验证码
        if (ConfigUtil.baseConfig().getBoolean("comm_authcode_flag", false)) {
            //验证万能验证码
            String commAuthcode = ConfigUtil.baseConfig().get("comm_authcode");
            //验证短信验证码
            String redisKey = StrUtil.format(ConfigUtil.redisKey().get("authCode_phone"), paramDict.getStr("phone"));
            String authCode = RedisUtil.get(redisKey);
            //真实验证码和短信验证码都有效
            if (!paramDict.getStr("authCode").equalsIgnoreCase(commAuthcode) &&
                        !paramDict.getStr("authCode").equalsIgnoreCase(authCode)) {
                throw new MyBaselogicException("107");
            }
        } else {
            //验证短信验证码
            String redisKey = StrUtil.format(ConfigUtil.redisKey().get("authCode_phone"), paramDict.getStr("phone"));
            String authCode = RedisUtil.get(redisKey);
            //验证是否失效
            if (StrKit.isBlank(authCode)) {
                throw new MyBaselogicException("108");
            }
            //验证验证码是否正确
            if (!authCode.equalsIgnoreCase(paramDict.getStr("authCode"))) {
                throw new MyBaselogicException("107");
            }
        }
        return ResultPoJo.create();
    }

    /**
     * 发送邮箱验证码
     *
     *
     * @param loginUser
     * @param email
     * @return
     * @author zhangby
     * @date 2018/9/24 下午5:27
     */
    public ResultPoJo sendEmailAuthCode(User loginUser, String email) {
        Prop emailConfig = ConfigUtil.emailConfig();
        Prop redisConfig = ConfigUtil.redisKey();
        //随机生成6位数字验证码
        String autoCode = CommonUtil.createAuthCode4(6, false);
        //存入redis
        RedisUtil.setStr(
                StrUtil.format(redisConfig.get("authCode_email"), email),
                autoCode,
                emailConfig.getInt("authcode_email_timeout"));
        //发送邮件
        SendEmailUtil.send(
                email,
                emailConfig.get("account_bind_subject"),
                StrUtil.format(emailConfig.get("account_bind_content"),loginUser.getName(),autoCode)
        );
        return ResultPoJo.create();
    }

    /**
     * 邮箱验证码验证
     * @param params 参数
     * @return
     */
    public ResultPoJo verifyEmailAuthCode(Dict params) {
        Prop redisConfig = ConfigUtil.redisKey();
        //获取redis数据
        String authCode = RedisUtil.get(StrUtil.format(redisConfig.get("authCode_email"), params.getStr("email")));
        if (StrUtil.isBlank(authCode)) {
            throw new MyBaselogicException("108");
        }
        if (!params.getStr("authCode").equals(authCode)) {
            throw new MyBaselogicException("107");
        }
        return ResultPoJo.create();
    }
}
